package com.migo.messageconverter;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Administrator on 2017/6/11 0011.
 */
public class CallbackMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
    private String callbackName;

    public void setCallbackName(String callbackName) {
        this.callbackName = callbackName;
    }

    public String getCallbackName() {
        return callbackName;
    }

    @Override
    protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String callbackParam = request.getParameter(callbackName);
        if(StringUtils.isEmpty(callbackParam)){
            super.writeInternal(object,outputMessage);
        }else{
            JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
            try{
                String result = callbackParam + "(" + super.getObjectMapper().writeValueAsString(object) + ");";
                IOUtils.write(result,outputMessage.getBody(),encoding.getJavaName());
            } catch (JsonProcessingException e){
                throw new HttpMessageNotWritableException("Could not write JSON: " + e.getMessage(), e);
            }
        }
    }
}
